package com.wunu.zn.api.appliance.model;
import common.plugin.mybatis.Db;
import common.plugin.mybatis.Model;
import java.util.Date;
import java.util.List;
import java.util.Map;

import common.AppConst;
import common.exception.BusinessException;
import common.plugin.mybatis.Table;
import common.utils.StringUtil;
import properties.ClientMsg;

@Table("t_wunu_appliance_user")
public class ApplianceUser  extends Model<ApplianceUser>{
	public static final String COLUMN_AGE = "age";

	/**
	 * @throws Exception ("账号不存在") ("密码错误")
	 */
	public static ApplianceUser login(String account, String password) throws BusinessException 
	{
		ApplianceUser user = findByAccount(account);

		
		if (user == null) 
		{
			throw new BusinessException(ClientMsg.USER_ACCOUNT_EXIST_NO, "账号不存在");
		}
		
		if (user.getStr("password").equals(password)) {
			//add by luke
			user.set("logincount", user.getInt("logincount")+1);
			user.set("lastLoginTime", new Date()).update();//更新每次的登录时间
		}
		
		return user;
	}
	
	
	/**
	 * 重置密码
	 * @param account
	 * @param password
	 */
	public static void resetPass(String account, String password) {
		ApplianceUser user = findByAccount(account);
		if (user != null) 
		{
			new ApplianceUser().set(COLUMN_ID, user.getId())
					  .set("password", password)
					  .update();
		}
	}
	/**
	 * 检查账号
	 * @param account
	 * @return
	 */
	public static ApplianceUser findByAccount(String account){
		return Db.findFirstByColumns(ApplianceUser.class, "account", account);
	}
	/**
	 * 检查昵称
	 * @param nickName
	 * @return
	 */
	public static ApplianceUser findByNickName(String nickName){
		return Db.findFirstByColumns(ApplianceUser.class, "nickName", nickName);
	}
	
	public static ApplianceUser findByUnionid(String unionid) {
		
		return Db.findFirstByColumns(ApplianceUser.class, "unionid", unionid);
	}
	
	


	public String getValidGender() 
	{
		String gender = getStr("gender");
		gender = gender == null ? AppConst.USER_GENDAR_DEFAULT : gender;
		return gender;
	}
	
	
	
	public static ApplianceUser findUserById(int userId) {
		ApplianceUser user = Db.findById(ApplianceUser.class, userId);
		if(user!=null){
			return user;
		}
		return new ApplianceUser();
	}
	/**
	 * 根据ID找用户信息
	 * @param String userId
	 * @return
	 */
	public static ApplianceUser findUserByIdStr(String userId) {
		ApplianceUser user = Db.findById(ApplianceUser.class, userId);
		if(user!=null){
			return user;
		}
		return new ApplianceUser();
	}
	public static void updateUser(ApplianceUser user) {
		Db.update(user);
	}
	
	
	/**
	 * 用户没输入个人信息时，使用默认
	 * 性别：男 
	 * 年龄：25 
	 * 昵称："" (不能为 null)
	 */
	public static void setDefaultInfo(ApplianceUser user) 
	{
		if (user.get("nickName") == null) 
		{
			user.set("nickName", "");
		}
		if (user.get("age") == null) 
		{
			user.set("age", AppConst.USER_AGE_DEFAULT);
		}
		
		if (user.get("gender") == null) 
		{
			user.set("gender", AppConst.USER_GENDAR_MALE);
		}
		
	}
	
	
	/**
	 * 查找所有用户
	 * @return
	 */
	public static List<Map<String, Object>> findAllUsers() {
		List<Map<String, Object>> list = Db.find("select * from t_wunu_appliance_user", null);
		return list;
	}


	public static boolean exist(int publicAccountUserId, String publicAccount) {
		ApplianceUser publicUser = Db.findById(ApplianceUser.class, publicAccountUserId, "account");
		return publicUser != null && publicUser.get("account") != null && publicUser.getStr("account").equals(publicAccount);
	}


	public static boolean isValidNickName(String nickName) {
		if (nickName == null || nickName.trim().isEmpty()) 
		{
			return false;
		}
		
		if (AppConst.UNVALID_NICK_NAME_LIST == null || AppConst.UNVALID_NICK_NAME_LIST.isEmpty()) 
		{
			return true;
		}
		
		nickName = nickName.trim();
		String[] unvalidArr = AppConst.UNVALID_NICK_NAME_LIST.split("，");
		for (String unvalidName : unvalidArr) 
		{
			if (nickName.contains(unvalidName)) 
			{
				return false;
			}
		}
		
		return true;
	}

	/**
	 * 保存获取ID
	 * @param user
	 * @return
	 */
	public static int addUser(ApplianceUser user) {
		Db.save(user);
		return user.getId();
	}
	
	/**
	 * 判断账号是否存在（EN）
	 * @param account
	 * @return
	 */
	public static boolean validAccount(String account){
		String sql = " SELECT *  FROM t_wunu_appliance_user WHERE account =#{account}  limit 1 ";
		 Map<String, Object> userInfo = Db.findFirst(sql, "account", account);
		 if(userInfo!= null){
			 return true;
		 }
		return false;
	}
	/**
	 * 
	 * @param user
	 * @throws Exception ("账号不能为空")("账号格式不正确")("账号已存在")
	 */
	public static void regist(ApplianceUser user) throws BusinessException{
		if (user.get("account") == null) 
		{
			throw new BusinessException(ClientMsg.USER_ACCOUNT_NULL, "账号不能为空");
		}
		
		if ( !StringUtil.isMobileNumber(user.getStr("account"))) 
		
		
		{
			throw new BusinessException(ClientMsg.USER_ACCOUNT_FORMAT_ERROR, "账号格式不正确");
		}
		
		boolean exit = ApplianceUser.validAccount(user.getStr("account"));
		
		if (exit)
		{
			throw new BusinessException(ClientMsg.USER_ACCOUNT_EXIST_YES, "账号已存在,请直接登录");
		}
		
		user.set("createTime", new Date());
		user.set("lastLoginTime", new Date());//更新每次的登录时间
		user.save();
	}
	/**
	 * 获取用户信息
	 * @param clintId
	 * @return
	 *//*
	public static User  finUserById(String clintId){
		String sql = " SELECT id, account, nickName, thirdPartId, accountType, photo, gender,skilllevel, age,"
				+ " hand, height, weight, createTime FROM t_t2f_user WHERE id =#{clintId}  limit 1 ";
		
		return  Db.findFirst(User.class,sql, "clintId", clintId);
		
	}*/
	/**
	 * 获取用户信息
	 * @param clintId
	 * @return
	 */
	public static ApplianceUser  finByClientId(String clintId){
		String sql = " SELECT  id, gender, nickName, photo FROM t_wunu_appliance_user WHERE id =#{clintId}  limit 1 ";
		
		return  Db.findFirst(ApplianceUser.class,sql, "clintId", clintId);
		
	}
	/**
	 * 模糊查找用户
	 * @return
	 */
	public static List<Map<String, Object>> findAll() {
		String sql = "select u.id, u.nickName  FROM t_wunu_appliance_user u  ";
		return Db.find(sql, "", "");
	}
	
	/**************************
	 *****   数据库操作   ******
	 **************************/
	
	public void save() 
	{
		Db.save(getMe());
	}
	
	public void update() 
	{
		Db.update(getMe());
	}
}
